package com.Wu.costomer.controller;

import com.Wu.costomer.model.Customer;
import com.Wu.costomer.service.CustomerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@WebServlet("/login.do")
public class CurstomerLoginController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //获取服务层对象
        CustomerService customerService = new CustomerService();
        //判断账号面膜是否能查询到会员对象
        Customer customer = customerService.login(username,password);
        if(customer != null){
            //获取session对象
            HttpSession session = req.getSession();
            //存储会员编号
            session.setAttribute("customerId",customer.getId());
            //存储用户账号
            session.setAttribute("username",customer.getUser_name());
            //存储用户的电话号码
            session.setAttribute("phone",customer.getMobile());

            LocalDateTime now = LocalDateTime.now();
            //定义日期时间的格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //格式会当前日期时间为指定格式
            String formattedDate = now.format(formatter);

            req.getRequestDispatcher("/index.html").forward(req, resp);
        }else {
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
    }
}
